package com.atguigu.day15.string01.exer;

import org.junit.Test;

/*
    字符串变量+字符串变量:
    字符串变量+字符串常量: new StringBuilder()
    字符串常量+字符串变量:
    -------------------
    字符串常量+字符串常量: 字符串常量

    "A"+"B"+"C"+"D":在内存中创建几个对象 1 个


 */
public class StringJVM {

    @Test
    public void test04(){

        String s = "杨幂";
        System.out.println("前 s = " + s);
        changeValue(s);
        System.out.println("后 s = " + s);
    }
    private void changeValue(String s) {
        s="范冰冰";
        System.out.println("中 s = " + s);
    }

    @Test
    public void test03() {
        String s1 = "Hello";

        String s2 = new String("Hello");
        /*
        s2.intern() 会查看常量池内是否有(equals()) "Hello" 如果有直接返回常量池中的地址
        如果没有,将Hello放到常量池内再返回
         */
        String intern = s2.intern();

        System.out.println(s1 == intern);//true

    }

    @Test
    public void test02() {
        final String s = "Hello";
        final String s1 = "World";

        String s2 = s + s1;
        //  System.out.println("s2 = " + s2);
        String s3 = "Hello" + s1;
        //System.out.println("s3 = " + s3);
        String s4 = s + "World";
        //System.out.println("s4 = " + s4);
        String s5 = "Hello" + "World";
        //System.out.println("s5 = " + s5);

        String s6 = "HelloWorld";

        System.out.println("s2==s6 = " + (s2 == s6));//true
        System.out.println("s3==s6 = " + (s3 == s6));//true
        System.out.println("s4==s6 = " + (s4 == s6));//true
        System.out.println("s5==s6 = " + (s5 == s6));//true
    }

    @Test
    public void test01() {
        String s = "Hello";
        String s1 = "World";

        String s2 = s + s1;
        //  System.out.println("s2 = " + s2);
        String s3 = "Hello" + s1;
        //System.out.println("s3 = " + s3);
        String s4 = s + "World";
        //System.out.println("s4 = " + s4);
        String s5 = "Hello" + "World";
        //System.out.println("s5 = " + s5);

        String s6 = "HelloWorld";

        System.out.println("s2==s6 = " + (s2 == s6));//true |false |
        System.out.println("s3==s6 = " + (s3 == s6));//true |false |
        System.out.println("s4==s6 = " + (s4 == s6));//true |false |
        System.out.println("s5==s6 = " + (s5 == s6));//true |false |true


    }
}
